package com.big.pay.controller;

import com.alibaba.fastjson.JSONObject;
import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.request.AlipayTradeFastpayRefundQueryRequest;
import com.alipay.api.request.AlipayTradeRefundRequest;
import com.alipay.api.response.AlipayTradeFastpayRefundQueryResponse;
import com.alipay.api.response.AlipayTradeRefundResponse;
import com.big.model.vo.pay.AlipayTradeQueryResponseVo;
import com.big.pay.service.PayService;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 *  支付宝支付，控制器
 *
 * @author wangziyang
 * */
@RestController
@RequestMapping(value = "/api/pay/al")
@AllArgsConstructor
public class PayController {

    @Autowired
    private PayService payService;


    /**
     *  下单支付
     * */
    @ApiOperation(value = "下单支付")
    @GetMapping(value = "/confirm/{oid}" )
    public Object pay (@PathVariable("oid") String oid) throws Exception {
        return payService.pay(oid);
    }
    /**
     *  支付成功的回调  @PathVariable("bizContent") String bizContents/{bizContent}
     * */
    @PostMapping(value = "/fallback/{oid}/{outTradeNo}")
    @ApiOperation(value = "")
    public Object fallback (@PathVariable("oid") String oid,
                            @PathVariable("outTradeNo") String outTradeNo) throws AlipayApiException {
        return  payService.fallback(oid,outTradeNo);
    }


    /**
     * 查询订单记录
     */
    @GetMapping(value = "/payInfo/{oid}")
    @ApiOperation(value = "查询订单记录")
    public AlipayTradeQueryResponseVo payInfo (@PathVariable("oid") String oid) throws Exception {
       return   payService.payInfo(oid);
    }

    /**
     * 退款
     */
    @GetMapping(value = "/refund")
    public Object refund () throws AlipayApiException {
        AlipayClient alipayClient = new DefaultAlipayClient("https://openapi.alipaydev.com/gateway.do",
                "2021000118611849",
                "MIIEvQIBADANBgkqhkiG9w0BAQEFAASCBKcwggSjAgEAAoIBAQCvkdRRYfTtGQ6BnjPfsRMwi6rPMsw/o7Z+K1O2womXZZuF2pYNs21bt3vUXyD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GPA3Ti891TtTrzKA9SNkDZYs1tq9TsT1xaWXpFK9dK+BWAg4npuH38cwFTQO1k8hb/PG4xpqH842J7dsMdwBQKBgANN8XmGwX4PgEE28NsSxuzHJNcbo/M31NQRLV5pRDeP8R4RqIUZtOP5Hu71QkXuUPJrahQm/D3bzp1cleWmMO6YeS61x60BeVz0WrXDFSF1vLab/rpUFYsl7ZrZPign0/ipim4RxqFaH3/tc5xSmk8OQOtMjkckMQu8E8z8TD4HAoGAdNjYbBhr7g6vekB8JxSwvk7xCwmzzA+nLrY93Ra0SAWAzSDxrLfalaspihDQa6SS0n0ki2qc8Cw0vqBOB60hx7MP4R0NKqXhqt9S8GHCfwI0OrKiJZBhnzMo6NuG1qejCPS+5fU0L4hms1PVrQ2S6NDuil8+2Z9pSGl5VmdA/hw=",
                "json",
                "utf-8",
                "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAqO2sM56t0f/G5aHipRy7WS8btZ3XS/GxUOmUt98dO7+QmZPU8c6eZCiXLqfwv6lGl07wVHTlaX1I5gr6XTWF1vxNihLiOx8OV66Y7GRFdTYjMGzG8KMbhepQfZDSID0c8IU5yGY+hk1ERXnpGD6kdMKOEjSl3c4lSkeYOXRsqxQnfkhm33jWsEzWF7up60UAT4f7VfpNJUQR8r87Rjsc2MQxkgwsJV8pV/vufjgUwkWcX1mch0aZp0F96PhLfCIQE7FhOLanmaad4V3c9xKoDtakbFud9QvqgoGm5dru58bm6NmXxugIYqrYLNSJG7xMfbMPEzzCDdn1sjjeldSQvwIDAQAB",
                "RSA2");
        AlipayTradeRefundRequest request = new AlipayTradeRefundRequest();
        JSONObject bizContent = new JSONObject();
        bizContent.put("trade_no", "2022041422001479150502566412");
        bizContent.put("refund_amount", 5);
        bizContent.put("out_request_no", "HZ01RF001");

//// 返回参数选项，按需传入
//JSONArray queryOptions = new JSONArray();
//queryOptions.add("refund_detail_item_list");
//bizContent.put("query_options", queryOptions);

        request.setBizContent(bizContent.toString());
        AlipayTradeRefundResponse response = alipayClient.execute(request);
        if(response.isSuccess()){
            System.out.println("调用成功");
        } else {
            System.out.println("调用失败");
        }
        return response.getBody();
    }


    /**
     * 退款查询
     */
    @GetMapping(value = "/refundInfo")
    public Object refundInfo () throws AlipayApiException {
        AlipayClient alipayClient = new DefaultAlipayClient("https://openapi.alipaydev.com/gateway.do",
                "2021000118611850",
                "MIIEvgIBADANBgkqhkiG9w0BAQEFAASCBKgwggSkAgEAAoIBAQCvMIIXH3c9lx2S9Khqi5FIFlmWt6c/5eGMpF5lxvdFDjQufenwXi9zFOUuQVrnZRt/zLctKw3fDYJF1xz/qHo7uGtqjQ15NFmjIJpJDCseMH+mCWiLX0ZtHCMwhxESrpeMy9fhMmb0h2q5GfxIib9OZy0mI1fZjYn436SFc5rPNNhHySat2sl6t2H8qojdGxWLXiJdIU8gjva5LntiS/sWhIR6Gh2wISxbkIvlG0nohTICGN+e26bBVZ5LEx5YokCPxT2CvxpOVZ4lbRAE0GtzIHdysW400cT9uGrzGAwz7uhS4wRZ+xb5i5tRE7zSkf5wGvnaX1XOoJxMTaikEEXdAgMBAAECggEAbKx8pndgcpZhRB38s0ssrjkfDf2TBHJHvgTggeYw0WeEOyrkibUDXjIiENV93Hi93ENThfNlQW6R110fogATAyTdjVU+fGdbC3RYAORJgwq2xHqlnX9WoqyHr7y5fItNe4ufuRSZBStTqstR0nWQdMRQH45hGpVQd0/UMOyLpeis7j8xsi7dBqRKcSB4hjWjGhakUseqDCZega256zC9p5oMMtz4f7zGVQExmrOIpcfGKMMDfbq3KMPda2RpuEEgZSsAKaJNF7sn5g+ox/PSFERemcS8R/EM43iKIkTm6rXhwP7qHJxqz60RAFe/wtnIPgOqU6O/mM7F3tVxmS8HSQKBgQDgOd3tR+1QjP9j2Vqef6yGsBWsc0G0mx7rMM/4OqEYL6gqSZ86KXq1adz+VjKeM6bJfvtt1bzZxUpJbrHDnPkJWS05Mqe/pbmsiyEc2HL1zCEr7z9yc5qJ4M7Hk7JQbKw4eBP3KltCKgO1eqJqSVJdpK+APoFsQKCkIXJpA8mbYwKBgQDIA8Ro20nUbYCsNFih/rxk0bnKzSV7pEcpFzjyWt71IskIw0tmhnQM0K7DbH6h806/dVegp/isPkHs+hc+atbj7TywUaMrSsTges05D3H83KUtxye9r9PYgrOf53hQniLCDDLXD0RNFWdTQkGDvMlTMO3COxF2+F4BU2awAwJ9vwKBgQDJZ9ELGId2yh+AnbTDF+VtWe8t5p1HT+qW0VKNIoSfB/eifFzUzntVRpDlt6Fh7v1Kko/fWZR3e7o9eXudPHuJRRvU+8Yn5AGClboZgebJGCFXqGAyOtq+LDswk18g1GxhkKJGajpslPad5VjL5a5hRNMLe5mfFfH1W8G7yFYoBwKBgD1ussfF+qItU+UQWty+odyRcfAxHD8ikNy1Dj+Z23sNSRPQ7/q4dzJschEB5M8XLOuvPc/RZ5Z3tMX1dmgsZn4xIn/BQ02LtHaCIUBSVx9kIbOyxKkFNRr94e/OJ20TdPeGyaK2RLMsTMUMx5SPyoIruAr9G3sgA7eChzquYfh/AoGBAKXTKIcZ5nQpeckQOntYsrJUPRNGVxQPznog7aQu80oJ0II2YXKQSfPTD2eBhaJOTXN3lIWSFHH5wYEUrRG+gnrfGil7TQWwQ3UGMNDNW1I+uSjokPvQ7SeHsIP+gRoBv/bzdMuUqpNthwCbgqFl7oR/L9kXYaT5WFcG5DfAX0xj",
                "json",
                "utf-8",
                "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAqO2sM56t0f/G5aHipRy7WS8btZ3XS/GxUOmUt98dO7+QmZPU8c6eZCiXLqfwv6lGl07wVHTlaX1I5gr6XTWF1vxNihLiOx8OV66Y7GRFdTYjMGzG8KMbhepQfZDSID0c8IU5yGY+hk1ERXnpGD6kdMKOEjSl3c4lSkeYOXRsqxQnfkhm33jWsEzWF7up60UAT4f7VfpNJUQR8r87Rjsc2MQxkgwsJV8pV/vufjgUwkWcX1mch0aZp0F96PhLfCIQE7FhOLanmaad4V3c9xKoDtakbFud9QvqgoGm5dru58bm6NmXxugIYqrYLNSJG7xMfbMPEzzCDdn1sjjeldSQvwIDAQAB",
                "RSA2");
        AlipayTradeFastpayRefundQueryRequest request = new AlipayTradeFastpayRefundQueryRequest();
        JSONObject bizContent = new JSONObject();
        bizContent.put("trade_no", "2022041422001479150502566650");
        bizContent.put("out_request_no", "HZ01RF001");

//// 返回参数选项，按需传入
//JSONArray queryOptions = new JSONArray();
//queryOptions.add("refund_detail_item_list");
//bizContent.put("query_options", queryOptions);
        request.setBizContent(bizContent.toString());
        AlipayTradeFastpayRefundQueryResponse response = alipayClient.execute(request);
        if (response.isSuccess()) {
            System.out.println("调用成功");
        } else {
            System.out.println("调用失败");
        }
        return response.getBody();
    }


}
